package cn.kooki.app.duobao.ui.Adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.data.Bean.User.record.RecordItem;
import cn.kooki.app.duobao.util.StringUtils;
import cn.kooki.app.duobao.util.TimeUtils;

/**
 * Created by xionghao on 15/12/27.
 */
public class DuobaoRecordAdapter extends BaseAdapter {

    public interface RecordOnClickWithTypeListener {
        void onItemClick(int position, int type);
    }

    private RecordOnClickWithTypeListener onClickWithTypeListener;

    public void setOnClickWithTypeListener(RecordOnClickWithTypeListener onClickWithTypeListener) {
        this.onClickWithTypeListener = onClickWithTypeListener;
    }

    private final boolean isother;
    private Context context;
    private ArrayList<RecordItem> datas;

    public DuobaoRecordAdapter(Context context, ArrayList<RecordItem> datas, boolean isother) {
        this.context = context;
        this.datas = datas;
        this.isother = isother;
    }


    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        RecordItem recordItem = datas.get(position);
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_duobao_item_record, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        int total = StringUtils.nullSafeParseInt(recordItem.zongrenshu);
        int currt = StringUtils.nullSafeParseInt(recordItem.canyurenshu);
        viewHolder.goodsTotal.setText("总需" + total);
        viewHolder.goodsRemain.setText("剩余" + (total - currt));
        viewHolder.goodsProgressFg.setMax(total);
        viewHolder.goodsProgressFg.setProgress(currt);
        viewHolder.duobaoRecordNum.setText(recordItem.q_num + "");
        viewHolder.duobaoRecordGname.setText(recordItem.title);

        viewHolder.goodsPeriod2.setText("参与期号 : " + recordItem.qishu);
        viewHolder.duobaoRecordAdd.setText(isother ? "跟买" : "追加");
        viewHolder.duobaoRecordAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickWithTypeListener != null) {
                    onClickWithTypeListener.onItemClick(position, 0);
                }
            }
        });
        viewHolder.duobaoRecordCheckMycode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickWithTypeListener != null) {
                    onClickWithTypeListener.onItemClick(position, 1);
                }
            }
        });
        Glide.with(context).load(recordItem.thumb).into(viewHolder.duobaoRecordPic);
        viewHolder.duobaoRecordCheckMycode.setText(isother ? "查看TA的号码" : "查看我的号码");
        int status = StringUtils.nullSafeParseInt(recordItem.status);
        if (status == 0) {
            viewHolder.goodsProgressFg.setVisibility(View.VISIBLE);
            viewHolder.duobaoRecordAdd.setVisibility(View.VISIBLE);
            viewHolder.duobaoRecordLine.setVisibility(View.VISIBLE);
            viewHolder.goodsRemain.setVisibility(View.GONE);
        } else if (status == 1) {
            viewHolder.duobaoRecordAdd.setVisibility(View.GONE);
            viewHolder.duobaoRecordOpenning.setVisibility(View.VISIBLE);
        } else if (status == 2) {
            viewHolder.duobaoRecordAdd.setVisibility(View.GONE);
            viewHolder.duobaoRecordLine.setVisibility(View.GONE);
            viewHolder.goodsProgressFg.setVisibility(View.GONE);
            viewHolder.duobaoRecordOpenning.setVisibility(View.GONE);
            viewHolder.goodsRemain.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(recordItem.q_uid)) {
            viewHolder.duobaoRecordOwnerNum.setText(recordItem.q_user_num + "");
            viewHolder.duobaoRecordOwnerCode.setText(recordItem.q_user_code);
            viewHolder.duobaoRecordOwnerName.setText(recordItem.q_user);
            viewHolder.duobaoRecordOwnerTime.setText(TimeUtils.getTime(recordItem.endtime));
            viewHolder.duobaoRecordOwner.setVisibility(View.VISIBLE);
        } else {
            viewHolder.duobaoRecordOwner.setVisibility(View.GONE);
        }
        return convertView;
    }


    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'layout_duobao_item_record.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.duobao_record_add)
        Button duobaoRecordAdd;
        @Bind(R.id.duobao_record_line)
        ImageView duobaoRecordLine;
        @Bind(R.id.duobao_record_gname)
        TextView duobaoRecordGname;
        @Bind(R.id.goods_progress_fg)
        ProgressBar goodsProgressFg;
        @Bind(R.id.goods_period2)
        TextView goodsPeriod2;
        @Bind(R.id.goods_period)
        TextView goodsPeriod;
        @Bind(R.id.goods_total)
        TextView goodsTotal;
        @Bind(R.id.goods_remain)
        TextView goodsRemain;
        @Bind(R.id.goods_progress_wappaer)
        RelativeLayout goodsProgressWappaer;
        @Bind(R.id.duobao_record_num)
        TextView duobaoRecordNum;
        @Bind(R.id.ic_follow)
        ImageView icFollow;
        @Bind(R.id.duobao_record_check_mycode)
        TextView duobaoRecordCheckMycode;
        @Bind(R.id.duobao_record_openning)
        LinearLayout duobaoRecordOpenning;
        @Bind(R.id.duobao_record_owner_name)
        TextView duobaoRecordOwnerName;
        @Bind(R.id.duobao_record_owner_num)
        TextView duobaoRecordOwnerNum;
        @Bind(R.id.duobao_record_owner_code)
        TextView duobaoRecordOwnerCode;
        @Bind(R.id.duobao_record_owner_time)
        TextView duobaoRecordOwnerTime;
        @Bind(R.id.duobao_record_owner)
        LinearLayout duobaoRecordOwner;
        @Bind(R.id.duobao_record_pic)
        ImageView duobaoRecordPic;
        @Bind(R.id.goods_free_label)
        ImageView goodsFreeLabel;
        @Bind(R.id.good_ten_label)
        ImageView goodTenLabel;
        @Bind(R.id.limit_duobao_id)
        ImageView limitDuobaoId;
        @Bind(R.id.custom_top_left_icon)
        ImageView customTopLeftIcon;
        @Bind(R.id.duobao_record_wrapper)
        RelativeLayout duobaoRecordWrapper;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
